<?php 
namespace addons\prl\service;
use addons\prl\model\Channels as ChannelsModel;
use addons\prl\model\Articles as ArticlesModel;
use fast\Tree;
/**
 * 
 */
class Channels
{

	//单列
	static public $instance;

	//所有栏目数组
	protected static $list = [];

	private function __construct()
	{
		//初始化栏目数组
		self::$list = (new ChannelsModel)->channel()->channelorder()->select();
	}

	public static function init()
	{
		if(!self::$instance) {
			//实力化自身
			self::$instance = new self();
		}
		return self::$instance;
	}

	public static function getListArr()
	{
		return collection(self::$list)->toArray();
	}

	/**
	 * 获取栏目树形数组
	 * @param  integer $id 栏目id
	 * @return array       栏目数组
	 */
	public static function getTreeListArr($id = 0)
	{
		$list = collection(self::$list)->toArray();
		$tree = Tree::instance();
		$tree->init($list, 'pid');
		return $tree->getTreeArray($id);
	}

	/**
	 * 获取模板html
	 * @param  string $itemtpl     模板字符串
	 * @param  string $selectedids 选中ids 可以为数组
	 * @param  string $disabledids 禁用ids 可以为数组
	 * @param  string $wraptag     子栏目下的包裹标签
	 * @return html                html格式的树形栏目
	 */
	public static function getTplTreeHtml($itemtpl, $selectedids = '', $disabledids = '', $wraptag = 'ul')
	{
		$selectedids = explode(",", $selectedids);
		$disabledids = explode(",", $disabledids);
		$list = collection(self::$list)->toArray();
		$tree = Tree::instance();
		$tree->init( $list, 'pid');
		$theme = $tree->getTreeUl(0, $itemtpl, $selectedids, $disabledids, $wraptag);
		return $theme;
	}

	/**
	 * 获取详情
	 * @param  string  $id    栏目id
	 * @param  integer $limit 取栏目下的文章条数 为0不获取文章
	 * @param  integer $page  页码数 当为文章时有用
	 * @return array         包含栏目信息 文章数组 分页总数
	 */
	public static function getInfo($id = '', $limit = 0, $page = 1)
	{
		$info = []; //栏目信息
		foreach (self::$list as $value) 
		{
			if($value['id'] == $id){
				$info = $value;
				break;
			}
		}
		$data = [];
		$count = 0;
		if ($limit > 0) { //如果获取limit大于0 就获取文章
			$data = ArticlesModel::scope('nomal,articlesorder')
			->where('channels_id', $info->id)
			->page($page, $limit)
			->select();
			$count = ceil(count($data) / $limit);
		}

		return ['info'=> $info, 'data' => ['list' => $data, 'count' => $count]];
	}

	/**
	 * 获取面包屑栏目
	 * @param  string $id 栏目id
	 * @return array     数组
	 */
	public static function getBreadList($id = '')
	{
		$list = collection(self::$list)->toArray();
		$tree = Tree::instance();
		$tree->init( $list, 'pid');
		$data = $tree->getParents($id, true);
		return $data;
	}

	/**
	 * 获取栏目各自下的文章列表
	 * @param  integer $id    父栏目id
	 * @param  integer $limit 数据条数		
	 * @param  integer $page  页码数
	 * @return array          包含栏目自生信息 和 各个栏目下的文章列表
	 */
	public static function getSelfListArr($id, $limit = 10, $page = 1)
	{
		$list = collection(self::$list)->toArray();
		$tree = Tree::instance();
		$tree->init($list, 'pid');
		$channel_arr = $tree->getChild($id); //栏目数组
		$data = [];
		foreach ($channel_arr as $value) {
			foreach ($list as $channel_row) {
				if($value['id'] == $channel_row->id){
					$arr['info'] = $value;
					$arr['data'] = [];
					if ($limit > 0) {
						$arr['data']['list'] = $channel_row->articles()->nomal()->articlesorder()->page($page, $limit)->select();
						$arr['data']['count'] = ceil($channel_row->articles()->nomal()->count() / $limit);
					}
					$data[] = $arr;
				}
			}
		}

		return $data;
	}

	/**
	 * 获取子栏目列表 并获取子栏目下 所有栏目的文章列表
	 * @param  integer $id    父栏目id
	 * @param  integer $limit 条数
	 * @param  integer $page  页码数
	 * @return array          包含栏目信息和文章列表
	 */
	public static function getAllListArr($id, $limit = 10, $page = 1)
	{
		$list = collection(self::$list)->toArray();
		$tree = Tree::instance();
		$tree->init($list, 'pid');
		$channel_ids = $tree->getChildrenIds($id); //子栏目所有id数组 不包含自生
		$data = $tree->getChild($id);//子栏目数组
		$list = ArticlesModel::where('id','in', $channel_ids)->nomal()->articlesorder()->page($page, $limit)->select();
		return ['info' => $data, 'list', $list];
	}

	/**
	 * 获取子栏目信息
	 * @param  int    $id      指定子栏目
     * @param  string field    字段
	 * @return array
	 */
	public static function getChannelChildrenList($id, $field)
	{
		$list = collection(self::$list)->toArray();
		$tree = Tree::instance();
		$tree->init($list, 'pid');
		$childChannelList = $tree->getChild($id);
		$fieldArr = explode(",", $field);
		$arr = [];
		foreach ($childChannelList as $value) {
			$tmpArr = [];
			foreach ($fieldArr as $f) {
				$tmpArr[$f] = isset($value[$f]) ? $value[$f] : '';
			}
			$arr[] = $tmpArr;
		}
		return $arr;
	}

}